<template>
  <!-- 导出/下载模板 组件 -->
  <el-button
    :type="type"
    :size="size"
    :loading="dwnLoading"
    icon="el-icon-download"
    @click="handleExport"
  >
    {{ dwnLoading ? `正在${text}...` : text }}
  </el-button>
</template>

<script>
import Cookies from "js-cookie";
import { clearNull } from "@/utils/ruoyi";
export default {
  props: {
    type: { type: String, default: "primary" }, //type
    size: { type: String, default: Cookies.get("size") || "medium" }, //size
    text: { type: String, default: "导出" }, //按钮上的文字
    title: { type: String, default: "默认标题" }, //导出文档的标题
    url: { type: String, default: "" }, //接口url
    obj: {
      type: Object,
      default() {
        return {};
      },
    }, //筛选字段
    formData: { type: Boolean, default: true }, //是否form表单, 默认为是
  },
  data() {
    return {
      dwnLoading: false,
    };
  },
  methods: {
    // 导出
    handleExport() {
      if (!this.url) {
        return this.$message.error("url为空");
      }
      let query = clearNull({
        ...this.obj,
      });
      this.dwnLoading = true;
      this.download(
        this.url,
        query,
        `${this.title}_${this.$dayjs().format("YYYY/MM/DD HH:mm:ss")}.xlsx`,
        this.formData,
      )
        .then((res) => {})
        .finally(() => {
          this.dwnLoading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped></style>
